package com.douqu.game.auth.web.request;

import com.douqu.game.core.database.model.OrderRecordModel;
import com.douqu.game.core.e.E_RechargeStatus;
import com.douqu.game.core.factory.PlatformFactory;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.web.request.RechargeParam;
import com.douqu.game.core.yjsdk.utils.MD5;

import javax.servlet.http.HttpServletRequest;

/**
* @author wangzhenfei
*         2018-01-17 16:38
*/
public class YjCheckOrderReqParam {
    private String app;
    private String cbi;
    private String ct;
    private String fee;
    private String pt;
    private String sdk;
    private String ssid;
    private String st;
    private String tcd;
    private String uid;
    private String ver;
    private String sign;

    private boolean isResultOk;

    private RechargeParam innerParam;

    public void init(HttpServletRequest request){
        StringBuffer sbEnc = new StringBuffer ();
        sbEnc.append ("app=");
        sbEnc.append (app = request.getParameter("app"));
        sbEnc.append ("&cbi=");
        sbEnc.append (cbi = request.getParameter("cbi"));
        sbEnc.append ("&ct=");
        sbEnc.append (ct = request.getParameter("ct"));
        sbEnc.append ("&fee=");
        sbEnc.append (fee = request.getParameter("fee"));
        sbEnc.append ("&pt=");
        sbEnc.append (pt = request.getParameter("pt"));
        sbEnc.append ("&sdk=");
        sbEnc.append (sdk = request.getParameter("sdk"));
        sbEnc.append ("&ssid=");
        sbEnc.append (ssid = request.getParameter("ssid"));
        sbEnc.append ("&st=");
        sbEnc.append (st = request.getParameter("st"));
        sbEnc.append ("&tcd=");
        sbEnc.append (tcd = request.getParameter("tcd"));
        sbEnc.append ("&uid=");
        sbEnc.append (uid = request.getParameter("uid"));
        sbEnc.append ("&ver=");
        sbEnc.append (ver = request.getParameter("ver"));
        sign = request.getParameter("sign");
        String localSign = MD5.encode(sbEnc + PlatformFactory.YJ_PRIVATE_KEY);
        LogUtils.info("本地秘钥：" + localSign + " 传过来的密钥:" + sign);
        isResultOk = localSign.equalsIgnoreCase(sign);
    }

    public String getTcd() {
        return tcd;
    }

    public boolean isResultOk() {
        return isResultOk;
    }

    public boolean isSuccess(){
        return "1".equals(st);
    }

    public RechargeParam getInnerParam() {
        if(innerParam == null){
            innerParam = new RechargeParam(cbi);
//            innerParam = JsonUtils.fromJSON(YjInnerParam.class, cbi);
        }
        return innerParam;
    }

    public int getFee() {
        return Integer.valueOf(fee);
    }

    public OrderRecordModel getOrderRecordModel(String paymentType){
        RechargeParam params =  getInnerParam();
        return new OrderRecordModel(params.getObjectIndex(), uid, ssid, tcd, sdk, getFee(), getInnerParam().getRechargeId(), getInnerParam().getServerId(),getInnerParam().getOutTradeNo(),
                params.getLv(),params.getDiamonds(),params.getRuleId(),paymentType, E_RechargeStatus.PAY_SUCCESS);
    }

    public String getSdk() {
        return sdk;
    }

    public String getCbi() {
        return cbi;
    }

    @Override
    public String toString() {
        return "YjCheckOrderReqDto{" +
                "app='" + app + '\'' +
                ", cbi='" + cbi + '\'' +
                ", ct=" + ct +
                ", fee=" + fee +
                ", pt=" + pt +
                ", sdk='" + sdk + '\'' +
                ", ssid='" + ssid + '\'' +
                ", st=" + st +
                ", tcd='" + tcd + '\'' +
                ", uid='" + uid + '\'' +
                ", ver='" + ver + '\'' +
                ", sign='" + sign + '\'' +
                '}';
    }

}
